package com.lpc.recyclerview;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.SparseArray;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class MainActivity extends AppCompatActivity {

    private RecyclerView rv;
    private List<String> lists = new ArrayList<>();
    private MyAdapter myAdapter;
    SparseArray<View> specials = new SparseArray<>();
    private TopSmoothScroller mScroller;
    private RecyclerView.LayoutManager layoutManager;
    private int indexOf;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        init();
    }

    private void init() {

        rv = findViewById(R.id.rv);

        //缓存设置(一、二、三级缓存)
        rv.setItemViewCacheSize(5);
        rv.setViewCacheExtension(new RecyclerView.ViewCacheExtension() {
            @Nullable
            @Override
            public View getViewForPositionAndType(@NonNull RecyclerView.Recycler recycler,
                                                  int position, int type) {

                //specials是在viewholder初始化中根据position进行添加view的
                return type == 0 ? specials.get(position) : null;
            }
        });
        rv.getRecycledViewPool().setMaxRecycledViews(0, 5);

        layoutManager = new LinearLayoutManager(this) {

            @Override
            public boolean canScrollVertically() {
                return super.canScrollVertically();//设置不可滑动则为false
            }
        };

        mScroller = new TopSmoothScroller(this);
        rv.setLayoutManager(layoutManager);

        for (int i = 0; i < 50; i++) {
            lists.add("lipc  " + i);
        }

        myAdapter = new MyAdapter(this, lists);
        rv.setAdapter(myAdapter);
    }

    /**
     * 滑动到指定iterm，可以设置时间------利用原生api
     *
     * @param view
     */
    public void click3(View view) {

        smoothMoveToPositionForInit(20);
    }

    /**
     * 滑动到指定iterm，可以设置时间------重写底层设置
     *
     * @param view
     */
    public void click2(View view) {

        mScroller.setTargetPosition(new Random().nextInt(50));
        layoutManager.startSmoothScroll(mScroller);
    }

    /**
     * 增删改或者移动的时候，都要同步更新list集合
     *
     * @param view
     */
    public void click(View view) {

//        View childAt = rv.getChildAt(indexOf++);
//        TextView tv = childAt.findViewById(R.id.tv);
//        Toast.makeText(this, tv.getText().toString(), Toast.LENGTH_SHORT).show();

        myAdapter.notifyItemChanged(8, "改变");

//        lists.set(8, "改变");
//        myAdapter.notifyItemChanged(8);

//        lists.set(8, "改变");
//        lists.set(9, "改变");
//        myAdapter.notifyItemRangeChanged(8, 2, lists);

//        myAdapter.notifyItemMoved(8, 9);

//        lists.remove(2);
//        lists.remove(2);
//        lists.remove(2);
//        myAdapter.notifyItemRangeRemoved(2, 3);
    }

    private void smoothMoveToPositionForInit(int position) {

        mShouldScroll = true;

        rv.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (mShouldScroll && RecyclerView.SCROLL_STATE_IDLE == newState) {
                    mShouldScroll = false;
                    smoothMoveToPosition(mToPosition);
                }
            }
        });

        if (position != -1) {
            smoothMoveToPosition(position);
        } else {
            smoothMoveToPosition(++position);
        }
        mToPosition = position;
    }

    //目标项是否在最后一个可见项之后
    private boolean mShouldScroll;
    //记录目标项位置
    private int mToPosition;

    /**
     * 滑动到指定位置
     *
     * @param position
     */
    private void smoothMoveToPosition(final int position) {

        // 第一个可见位置
        int firstItem = rv.getChildLayoutPosition(
                rv.getChildAt(0));
        // 最后一个可见位置
        int lastItem = rv.getChildLayoutPosition(
                rv.getChildAt(
                        rv.getChildCount() - 1));
        if (position < firstItem) {
            // 第一种可能:跳转位置在第一个可见位置之前
            rv.smoothScrollToPosition(position);
        } else if (position <= lastItem) {
            // 第二种可能:跳转位置在第一个可见位置之后
            int movePosition = position - firstItem;
            if (movePosition >= 0 && movePosition < rv.getChildCount()) {
                int top = rv.getChildAt(movePosition).getTop();
                rv.smoothScrollBy(0, top);
            }
        } else {
            // 第三种可能:跳转位置在最后可见项之后
            rv.smoothScrollToPosition(position);
            mShouldScroll = true;
        }
    }
}
